package Servlet;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 不能把这一部分加入MyServlet，因为这是用Java代码生成的图片，而MyServlet继承了MyBaseServlet，基类里设置了中文编码，会造成乱码
 */
@WebServlet("/code")
public class code extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();//开启会话
        ValidateCode validateCode = new ValidateCode(400, 200, 4, 50);
        String code = validateCode.getCode();
        session.setAttribute("yanzheng",code);//机器生成的验证码，共享
        System.out.println(code);
        validateCode.write(resp.getOutputStream());
    }
}
